#include "echoServer.h"

int EchoServer::process(InputBuffer &ib, OutputBuffer &ob)
{
    string 	funcName;
	int 	argCnt;
	string 	ret;
	
    cout << ib._len << endl;
    const char *pc = ib._buf;
    for (int ix=0; ix<ib._len; ++ix)
        cout << "[" << ix << "]: " << (int)pc[ix] << endl;

	ib.read(funcName);
	ib.read(argCnt);
	if (funcName == "echo" && argCnt == 2)
    {
		string input, output;
		ib.read(input);
		ib.read(output);
		ret = echo(input, output);
		
		ob.write(_servName);
		ob.write(funcName);
		ob.write(ret);
		ob.write(output);
		
        return 0;
    }
    else
        return -1;
}

string& EchoServerProxy::echo(const string &input, string &output)
{
    cout << "in EchoServerProxy::echo" << endl;

	InputBuffer ib;
	OutputBuffer ob;
	string funcName, ret;
	
	ob.write(_servName);
    funcName = "echo";
	ob.write(funcName);
	ob.write(2);
	ob.write(input);
	ob.write(output);

    const char *pc = ob.getBuf();
    int len = ob.getLen();
    cout << len << endl;
    for (int ix=0; ix<len; ++ix)
        cout << "[" << ix << "]: " << (int)pc[ix] << endl;
	
    cout << "before invoke" << endl;
	invoke(ib, ob);		// need check return value
    cout << "after invoke" << endl;
	
	ib.read(funcName);	// need check the funcName, throw Exception
	ib.read(ret);
	ib.read(output);
	
    return output;  	// return the ret
}
